
var editor = null;

// for judging
var results = [];
var resultTable = null;
var resultTablebody = null;
var head = ["Status", "Input", "Output", "Expected"];
var ojFunction = null;
var functionName = "";
var currentStatus = "";
var compileMsg = "";
var compileErrLine = 0;

function initResults(){

    var mybody  = document.getElementsByTagName("body")[0];
    try{
        mybody.removeChild(resultTable);
    }catch (e) {
    }
    resultTable = document.createElement("table");
    resultTablebody = document.createElement("tbody");
    resultTable.className = "altrowstable";

    mycurrent_row = document.createElement("tr");
    for(var i = 0; i < head.length; i++) {
        mycurrent_cell = document.createElement("th");
        currenttext = document.createTextNode(head[i]);
        mycurrent_cell.appendChild(currenttext);
        mycurrent_row.appendChild(mycurrent_cell);
    }
    mycurrent_row.className = "oddrowcolor";
    resultTablebody.appendChild(mycurrent_row);
}

function getInputAsString(input){
    var ret = "";
    for(var i in input){
        if(input[i].length > 0){
            ret += "[";
            ret += input[i].toString();
            ret += "]";
        }else{
            ret += input[i].toString();
        }
        if(i != input.length - 1){
            ret += ", ";
        }
    }
    return ret;
}

function showStatus(){

    var statusDiv = document.getElementById("status");
    while (statusDiv.hasChildNodes()) {
        statusDiv.removeChild(statusDiv.lastChild);
    }

    var output = document.createElement("div");
    output.id = "outputStatus";
    output.className = "statusOutput";
    var status = document.createTextNode("Run Status: ");
    output.appendChild(status);
    statusDiv.appendChild(output);

    var stat = document.createElement("span");
    stat.className = currentStatus == "Accepted"?"statusAC":"statusError";
    status = document.createTextNode(currentStatus);
    stat.appendChild(status);
    output.appendChild(stat);

    if(currentStatus == "Compile Error"){
        var comMsg = document.createElement("pre");
        comMsg.id = "compileStatus";
        comMsg.className = "statusCompileMsg";
        var msg = document.createTextNode(compileMsg);
        comMsg.appendChild(msg);
        statusDiv.appendChild(comMsg);
    }

    var br = document.createElement("br");
    statusDiv.appendChild(br);
}

function showResults(){

    if(ojFunction == null)
        return;

    var mybody = document.getElementsByTagName("body")[0];
    for(var u in test_units) {
        var ret = ojFunction(test_units[u][0][0],test_units[u][0][1]);
        var tu = getInputAsString(test_units[u][0]);

        var expected = test_units[u][1].toString();
        var answer = ret.toString();
        var status = expected == answer?"Pass":"Fail";

        var row = [status,tu,answer,expected];

        mycurrent_row = document.createElement("tr");
        for(var i = 0; i < row.length; i++) {
            mycurrent_cell = document.createElement("td");
            currenttext = document.createTextNode(row[i]);
            mycurrent_cell.appendChild(currenttext);
            mycurrent_row.appendChild(mycurrent_cell);
        }
        if(u % 2 == 0){
            mycurrent_row.className = "evenrowcolor";
        }else{
            mycurrent_row.className = "oddrowcolor";
        }
        resultTablebody.appendChild(mycurrent_row);
    }
    resultTable.appendChild(resultTablebody);
    mybody.appendChild(resultTable);
}

function judge(){

    ojFunction = null;
    initResults();

    var code = editor.getValue();
    try {
        eval(code);
    } catch (e) {
        var err = e.constructor('Error in Evaled Script: ' + e.message);
        compileErrLine = e.lineNumber - err.lineNumber + 3;

        compileMsg = "Line ";
        compileMsg += compileErrLine;
        compileMsg += ": ";
        compileMsg += e.message;

        currentStatus = "Compile Error";
        showStatus();
        return;
    }

    var stat = "ojFunction = ";
    stat += functionName;
    eval(stat);

    showResults();
}